package spzx.controller;

import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.base.ProductUnit;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spzx.service.ProductService;

import java.util.List;
import java.util.Map;

/**
 * 有关商品列表的所有接口
 */
@RestController
@RequestMapping("admin/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    /**
     * 商品的上下架
     * @param spuId
     * @param status
     * @return
     */
    @GetMapping("updateStatus/{spuId}/{status}")
    public Result updateStatus(@PathVariable Long spuId, @PathVariable Integer status){
        productService.updateStatus(spuId, status);
        return Result.ok();
    }


    /**
     * 获取审核状态的商品列表
     * params: auditStatus 审核状态
     * @return Result
     */
    @GetMapping("updateAuditStatus/{spuId}/{auditStatus}/{reason}")
    public Result updateAuditStatus(@PathVariable Long spuId, @PathVariable Integer auditStatus, @PathVariable String reason){
        productService.updateAuditStatus(spuId, auditStatus, reason);
        return Result.ok();
    }

    /**
     * 根据Id删除商品
     * @param spuId
     * @return
     */
    @DeleteMapping("deleteById/{spuId}")
    public Result deleteById(@PathVariable Long spuId){
        productService.deleteById(spuId);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 根据商品id查询商品详情(spu + sku列表 + detail)
     * @param productId
     * @return
     */
    @GetMapping("findById/{productId}")
     public Result findById(@PathVariable Long productId){
         Product product = productService.findById(productId);
         return Result.build(product, ResultCodeEnum.SUCCESS);
     }

    /**
     * 修改商品
     *
     */
    @PutMapping("updateProduct")
    public Result updateProduct(@RequestBody Product product){
        productService.updateProduct(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }


    /**
     * 增加商品
     * @return
     */
    @PostMapping("addProduct")
    public Result addProduct(@RequestBody Product product) {
        productService.addProduct(product);
        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    /**
     * 查询所有商品单位
     * @return
     */
    @GetMapping("findAllProductUnit")
    public Result findAllProductUnit(){
        List<ProductUnit> list = productService.findAllProductUnit();
        return Result.build(list, ResultCodeEnum.SUCCESS);
    }


    @GetMapping("/findByPage/{pageNum}/{pageSize}")
    public Result findByPage(@PathVariable Integer pageNum,@PathVariable Integer pageSize, ProductDto productDto){
        PageInfo pageInfo = productService.findByPage(pageNum, pageSize, productDto);
        return Result.build(pageInfo, ResultCodeEnum.SUCCESS);
    }


}
